var ns, toast;
app = new Vue({
    el: '#app',
    data: {
        payCost: 0, //应付金额
        totalCost: 0,
        totalCount: 0, //账单数
        discountCost: 0, //优惠金额
        breakCost: 0, //违约金
        payType: 1, //线下付款 2在线付款
        payOnlineType: 1, //1=微信支付 2=支付宝支付
        idlist: '',
        QrCodeUrl: '',
        TradeNo: 0,
        paymentId: 0,
        paySucceed: false,
        paymentMethods: [],
        // paymentMethods:[{"id":1,"name":"刷卡"},{"id":3,"name":"冲抵"},{"id":4,"name":"转账"},{"id":6,"name":"减免"},{"id":7,"name":"微信支付"},{"id":8,"name":"现金"},{"id":9,"name":"支付宝"},{"id":11,"name":"历史收款"},{"id":13,"name":"转账"},{"id":14,"name":"建行龙支付"}],
        chargeMoneyTypeId: 0,
        chargeMoneyTypeName: '',
        checkPayTimeout: null,
        wechatPayEnable: false,
        alipayEnable: false,
        totalPaidCost: 0,
        IsSureCost: false,
        TotalBreakCosts: 0, //违约金金额-操作
        TotalPayCosts: 0, //总金额-操作
        BreakCostList: [{
            id: 1,
            name: '全额收取',
            description: '收取已选账单的违约金金额。'
        }, {
            id: 2,
            name: '本次不收取',
            description: '本次不收取违约金，违约金将生成独立的应收账单。'
        }, {
            id: 3,
            name: '免除违约金',
            description: '本次不收取违约金，收款完成后删除违约金记录。'
        }, ], //违约金处理
        BreakCostID: 1,
        breakform: {
            id: 1,
            name: '全额收取',
            description: '收取已选账单的违约金金额。'
        }, //选中记录
    },
    methods: {
        get_data: function() {
            //获取付费账单
            var that = this;
            ns.post({
                action: 'getqrpayroomfeelist',
                idlist: that.idlist,
            }, function(succeed, data, err) {
                if (succeed) {
                    that.totalCost = data.totalCost;
                    that.payCost = data.totalPayCost;
                    that.TotalPayCosts = data.totalPayCost;
                    that.totalCount = data.totalPayCount;
                    that.discountCost = data.totalDiscountCost;
                    that.breakCost = data.totalBreakCost;
                    that.TotalBreakCosts = data.totalBreakCost;
                    that.totalPaidCost = data.totalPaidCost;
                    that.paymentMethods = data.paymentMethods;
                    if (that.paymentMethods.length > 0) {
                        that.doOffPay(that.paymentMethods[0]);
                    }
                    that.alipayEnable = data.alipayEnable;
                    that.wechatPayEnable = data.wechatPayEnable;
                    if (that.wechatPayEnable) {
                        that.payOnlineType = 1;
                    } else if (that.alipayEnable) {
                        that.payOnlineType = 2;
                    }
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        changeBreakCost: function(item) {
            var that = this;
            that.breakform = item;
            if (item.id == 1) {
                that.breakCost = that.TotalBreakCosts;
                that.payCost = that.TotalPayCosts;
            } else {
                that.breakCost = 0;
                that.payCost = that.TotalPayCosts - that.TotalBreakCosts;
            }
        },
        doSwitchType: function(type) {
            var that = this;
            that.payType = type;
            if (that.checkPayTimeout) {
                clearTimeout(that.checkPayTimeout);
                that.checkPayTimeout = null;
            }
            if (type == 2) {
                that.do_generate(that.payOnlineType);
            }
        },
        doOffPay: function(item) {
            this.chargeMoneyTypeId = item.id;
            this.chargeMoneyTypeName = item.name;
        },
        do_generate: function (type) {
            var that = this;
            that.payOnlineType = type;
            if (that.paySucceed) {
                return;
            }
            if (Number(that.payCost) == 0) {
                api.toast({
                    msg: '金额为0不能支付',
                    duration: 2000,
                    location: 'bottom'
                });
                return
            }
            if (that.payOnlineType == 0) {
                api.toast({
                    msg: '请选择支付方式',
                    duration: 2000,
                    location: 'bottom'
                });
            } else if (that.payOnlineType == 1) {
                ns.post({
                    action: 'wxpayorderbyqrcodeready',
                    idlist: that.idlist,
                }, function (succeed, data, err) {
                    if (succeed) {
                        that.QrCodeUrl = data.QrCodePath;
                        that.TradeNo = data.TradeNo;
                        that.paymentId = data.paymentId;
                        that.confirm_pay();
                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            } else if (that.payOnlineType == 2) {
                ns.post({
                    action: 'alipayorderbyqrcodeready',
                    idlist: that.idlist,
                }, function (succeed, data, err) {
                    if (succeed) {
                        that.QrCodeUrl = data.QrCodePath;
                        that.TradeNo = data.TradeNo;
                        that.paymentId = data.paymentId;
                        that.confirm_pay();

                    } else {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
            } else if (that.pay_type == 3) {
                api.toast({
                    msg: '请使用现金付款',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        },
        confirm_pay: function() {
            var that = this;
            if (that.checkPayTimeout) {
                clearTimeout(that.checkPayTimeout);
                that.checkPayTimeout = null;
            }
            console.log(JSON.stringify(that.payCost));
            ns.post({
                action: 'checkqrcodepaystatus',
                TradeNo: that.TradeNo,
                payCost: that.payCost,
            }, function(succeed, data, err) {
                if (succeed) {
                    that.payComplete();
                } else {
                    that.checkPayTimeout = setTimeout(function() {
                        that.confirm_pay();
                    }, 2000)
                }
            });
        },
        payComplete: function() {
            var that = this;
            if (that.checkPayTimeout) {
                clearTimeout(that.checkPayTimeout);
                that.checkPayTimeout = null;
            }
            that.paySucceed = true;
            api.sendEvent({
                name: 'updatewuyejf'
            })

            //收款成功后跳转页面
            if (that.paySucceed) {
                ns.openWin('shoufeipay_success', '支付成功', {
                    paymentId: that.paymentId
                });
            }
        },
        doOpenPrint: function() {
            var that = this;
            ns.openWin('shoufeipay_confirm_frm', '打印收款单据', {
                paymentId: that.paymentId
            });
        },
        cancel_pay: function() {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确认取消付款？',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    api.toast({
                        msg: '已经取消本次付款',
                        duration: 2000,
                        location: 'bottom'
                    });
                    if (that.pay_Interval != null) {
                        clearTimeout(that.pay_Interval);
                    }
                }
            });
        },
        openWin: function(item, params) {
            var that = this;
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_save: function() {
            var that = this;
            that.IsSureCost = true;
        },
        payroomfeeoffline: function() {
            var that = this;
            that.IsSureCost = false;
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function(ret) {});
            ns.post({
                action: 'payroomfeeoffline',
                idlist: that.idlist,
                chargeMoneyTypeId: that.chargeMoneyTypeId,
                BreakCostID: that.BreakCostID,
            }, function(succeed, data, err) {
                toast.hide();
                if (succeed) {
                    that.TradeNo = data.TradeNo;
                    that.paymentId = data.paymentId;

                    that.confirm_pay();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        doCloseSureCost: function() {
            var that = this;
            that.IsSureCost = false;
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.payCost = api.pageParam.payCost;
    app.totalCost = api.pageParam.totalCost;
    app.totalCount = api.pageParam.totalCount;
    app.discountCost = api.pageParam.discountCost;
    app.breakCost = api.pageParam.breakCost;
    app.idlist = api.pageParam.idlist || '';
    app.get_data();
};
